package com.cloud.alibaba.common.util;

import com.alibaba.excel.EasyExcel;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author taoxinhuan
 * @description: 工具类，现在只做了导出，以后慢慢完善工具类吧
 * @date 2020/9/3 17:57
 */
public class EasyExcelUtil {

    public static <T> void exportExcelData(HttpServletResponse response, List<T> entityList, String fileName, String sheetName,Class clazz) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), clazz).sheet(sheetName).doWrite(entityList);
    }
}
